package com.bgds.sys.module.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bgds.sys.common.constant.BizConstant;
import com.bgds.sys.common.constant.StatusConstant;
import com.bgds.sys.common.pagevo.PageVo;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.AlarmLog;
import com.bgds.sys.module.entity.LogHandleResult;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import com.bgds.sys.module.service.ILogHandleResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 公共前端接口类
 * </p>
 *
 * @author lj
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/alarmLog")
@Api(tags = "报警日志接口")
public class AlarmLogController {

    @Autowired
    private IAlarmLogService iAlarmLogService;

    @Autowired
    private ILogHandleResultService iLogHandleResultService;

    @Resource
    private  IDeviceDynamicAttrService iDeviceDynamicAttrService;


    /**
     * @param startTime
     * @param endTime
     * @param page
     * @param state
     * @param deviceName
     * @param attrName
     * @param alarmLevel
     * @return
     */

    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ApiOperation(value = "按条件分页查询设备报警信息", notes = "按条件分页查询设备报警信息", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", dataType = "Long", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", dataType = "Long", value = "结束时间"),
            @ApiImplicitParam(name = "state", dataType = "Integer", value = "状态"),
            @ApiImplicitParam(name = "alarmLevel", dataType = "Integer", value = "报警等级"),
    })
    public Result<IPage<AlarmLogDto>> getWarnLogPage(Long startTime, Long endTime, @ModelAttribute PageVo page,
                                                     @RequestParam(required = false) Integer state  ,
                                                     @RequestParam(required = false)  String deviceName,
                                                     @RequestParam(required = false)  String attrName,
                                                     @RequestParam(required = false) Integer alarmLevel) {
        return new ResultUtil<IPage<AlarmLogDto>>().setData(iAlarmLogService.queryForPage(startTime, endTime, state, deviceName, attrName, alarmLevel, page));
    }


    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value = "忽略报警信息", notes = "忽略报警信息", httpMethod = "PUT", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Long", required = true, value = "报警信息ID")

    })
    public Result<Object> updateWarnLog(Long id) {
        AlarmLog alarmLog = iAlarmLogService.getById(id);
        if (alarmLog == null) {
            return new ResultUtil<>().setErrorMsg(201, "处理失败！");
        }
        alarmLog.setStatus(StatusConstant.WARM_LOG_HANDLED_STATUS);
        alarmLog.setProcessingTime(System.currentTimeMillis());
        Boolean flag = iAlarmLogService.updateById(alarmLog);

        //更新属性表报警状态为正常
        boolean b   =  iDeviceDynamicAttrService.updateAlarmStatus(alarmLog.getAttrId(),BizConstant.ZERO);
        if (flag&&b) {
            return new ResultUtil<>().setErrorMsg(200, "处理成功！");
        }
        return new ResultUtil<>().setErrorMsg(201, "处理失败！");
    }

    @RequestMapping(value = "/handleResult", method = RequestMethod.POST)
    @ApiOperation(value = "保存报警信息处理结果", notes = "保存报警信息处理结果", httpMethod = "POST")
    public Result<Object> savehandleResult(@RequestBody LogHandleResult logHandleResult) {
        AlarmLog alarmLog = iAlarmLogService.getById(logHandleResult.getLogId());
        alarmLog.setStatus(StatusConstant.WARM_LOG_HANDLED_STATUS);
        alarmLog.setAlarmLevel(logHandleResult.getLevel());
        alarmLog.setProcessingTime(System.currentTimeMillis());
        iAlarmLogService.updateById(alarmLog);
        logHandleResult.setHandleTime(System.currentTimeMillis());
        Boolean flag = iLogHandleResultService.saveOrUpdate(logHandleResult);

        //更新属性表报警状态为正常
        boolean b =  iDeviceDynamicAttrService.updateAlarmStatus(alarmLog.getAttrId(),BizConstant.ZERO);
        if (flag&&b) {
            return new ResultUtil<>().setSuccessMsg("保存成功");
        }
        return new ResultUtil<>().setSuccessMsg("保存失败");
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value = "报警信息id获取处理详情", notes = "报警信息id获取处理详情", httpMethod = "GET")
    public Result<LogHandleResult> getByLogId(Long logId) {
        LogHandleResult logHandleResult = iLogHandleResultService.getByLogId(logId);
        return new ResultUtil<LogHandleResult>().setData(logHandleResult);
    }


}


